//
//  AddViewController.swift
//  MyNotesDemo
//
//  Created by BZF on 2020/8/20.
//  Copyright © 2020 bzf. All rights reserved.
//

import UIKit

class AddViewController: UIViewController,UITextViewDelegate {

    @IBOutlet weak var contentTV: UITextView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.contentTV.delegate = self
        self.contentTV.becomeFirstResponder()
    }
    

    @IBAction func cancelListener(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    
    
    @IBAction func saveListener(_ sender: Any) {
        let note = ENote()
        note.date = NSDate()
        note.content = self.contentTV.text
        
        let dao = NoteDAO.sharedInstance
        dao.insert(note)
        let listData = dao.findAll()
        
        //发送重新加载备忘录列表的通知
        NotificationCenter.default.post(name: Notification.Name("reloadViewNotification"), object: listData)
        
        //放弃第一响应者(才能自动关闭键盘)
        self.contentTV.resignFirstResponder()
        
        self.dismiss(animated: true, completion: nil)
    }
    
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if(text == "\n"){
            print("TextView获得焦点，点击return返回")
            contentTV.resignFirstResponder()
            return false
        }
        return true
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
